package com.nq.controller;


import com.nq.common.ServerResponse;

import com.nq.pojo.SiteSpread;
import com.nq.service.ISiteSettingService;
import com.nq.service.ISiteSpreadService;
import com.nq.service.IStAllocationSettingService;
import com.nq.service.IUserService;

import com.nq.service.impl.StAllocationSettingServiceImpl;
import com.nq.utils.PropertiesUtil;

import com.nq.utils.redis.CookieUtils;

import com.nq.utils.redis.JsonUtil;

import com.nq.utils.redis.RedisConst;

import com.nq.utils.redis.RedisShardedPoolUtils;

import com.nq.vo.user.UserLoginResultVO;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;


@Controller
@RequestMapping({"/api/user/"})
public class UserApiController {
    private static final Logger log = LoggerFactory.getLogger(UserApiController.class);

    @Autowired
    IUserService iUserService;

    @Autowired
    ISiteSpreadService iSiteSpreadService;
    @Autowired
    ISiteSettingService iSiteSettingService;

    @Autowired
    IStAllocationSettingService stAllocationSettingService;

    /**
     * 注册
     * @param agentCode
     * @param regType
     * @param account
     * @param yzmCode
     * @param userPwd
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = {"reg.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse reg(@RequestParam(value = "agentCode", required = false) String agentCode,
                              @RequestParam(value = "code", required = false) String code,
                              @RequestParam("regType") String regType,
                              @RequestParam("account") String account,
                              @RequestParam(value = "yzmCode", defaultValue = "") String yzmCode,
                              @RequestParam("userPwd") String userPwd,
                              @RequestParam(value = "country", required = false) String country,
                              @RequestParam(value = "inviteCode", required = false) Integer inviteCode,
                              HttpServletRequest httpServletRequest) {
        return this.iUserService.reg(yzmCode, code, agentCode, regType, account, userPwd, country, inviteCode, httpServletRequest);
    }

    /**
     * 登录
     * @param loginType
     * @param account
     * @param userPwd
     * @param httpSession
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = {"login.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse login(@RequestParam("loginType") String loginType,
                                @RequestParam(value = "code", required = false) String code,
                                @RequestParam("account") String account,
                                @RequestParam("userPwd") String userPwd,
                                @RequestParam(value = "country", required = false) String country,
                                HttpSession httpSession,
                                HttpServletRequest request, HttpServletResponse response) {
        String pc_cookie_name = PropertiesUtil.getProperty("user.cookie.name");
        String token = RedisConst.getUserRedisKey(httpSession.getId());
        ServerResponse serverResponse = this.iUserService.login(loginType, code, account, userPwd, country, request);
        if (serverResponse.isSuccess()) {
            CookieUtils.writeLoginToken(response, token, pc_cookie_name);
            String redisSetExResult = RedisShardedPoolUtils.setEx(token, JsonUtil.obj2String(serverResponse.getData()), 9999);
            log.info("redis setex user result : {}", redisSetExResult);
            UserLoginResultVO resultVO = new UserLoginResultVO();
            resultVO.setKey(pc_cookie_name);
            resultVO.setToken(token);
            return ServerResponse.createBySuccess("Login successfully", resultVO);
        }
        return serverResponse;
    }

    //注销
    @RequestMapping({"logout.do"})
    @ResponseBody
    public ServerResponse logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String cookie_name = PropertiesUtil.getProperty("user.cookie.name");
        String logintoken = CookieUtils.readLoginToken(httpServletRequest, cookie_name);
        log.info("user token = {}, Exit login", logintoken);
        RedisShardedPoolUtils.del(logintoken);
        CookieUtils.delLoginToken(httpServletRequest, httpServletResponse, cookie_name);
        return ServerResponse.createBySuccess();
    }

    /**
     * 校验验证码
     * @param type
     * @param yzmCode
     * @param account
     * @return
     */
    @RequestMapping({"checkYzmCode.do"})
    @ResponseBody
    public ServerResponse checkYzmCode(String type, String yzmCode, String code, String account) {
        return this.iUserService.checkYzmCode(type, yzmCode, code ,account);
    }


    /**
     * 忘记密码-校验验证码
     * @param type
     * @param yzmCode
     * @param account
     * @return
     */
    @RequestMapping({"checkForgetYzmCode.do"})
    @ResponseBody
    public ServerResponse checkForgetYzmCode(String type, String yzmCode, String code, String account) {
        return this.iUserService.checkForgetYzmCode(type, yzmCode, code, account);
    }

    /**
     * 设置支付密码
     * @param payPwd
     * @return
     */
    @RequestMapping({"setPayPwd.do"})
    @ResponseBody
    public ServerResponse setPayPwd(HttpServletRequest request, String payPwd) {
        return this.iUserService.setPayPwd(request, payPwd);
    }


    /**
     * 校验支付密码
     * @param request
     * @param currPayPwd 当前密码
     * @return
     */
    @RequestMapping({"checkPayPwd.do"})
    @ResponseBody
    public ServerResponse checkPayPwd(HttpServletRequest request, String currPayPwd) {
        return this.iUserService.checkPayPwd(request, currPayPwd);
    }


    /**
     * 找回密码
     * @param type
     * @param code
     * @param account
     * @param newPwd
     * @return
     */
    @RequestMapping({"updatePwd.do"})
    @ResponseBody
    public ServerResponse updatePwd(String type, String code, String account, String newPwd) {
        return this.iUserService.updatePwd(type, code, account, newPwd);
    }

    /**
     * 查询点差费率
     * @author lr
     * @date 2020/07/01
     * applies：涨跌幅
     * turnover：成交额
     * code:股票代码
     * unitprice：股票单价
     **/
    @RequestMapping({"findSpreadRateOne.do"})
    @ResponseBody
    public ServerResponse findSpreadRateOne(BigDecimal applies, BigDecimal turnover, String code, BigDecimal unitprice) {
        SiteSpread siteSpread = this.iSiteSpreadService.findSpreadRateOne(applies,turnover,code,unitprice);
        return ServerResponse.createBySuccess("success", siteSpread);
    }

    @RequestMapping({"getSetting.do"})
    @ResponseBody
    public ServerResponse getSetting() {
        return ServerResponse.createBySuccess(this.iSiteSettingService.getSiteSetting());
    }

    /**
     * 绑定上级
     * @param request
     * @param account
     * @return
     */
    @RequestMapping({"bindParent.do"})
    @ResponseBody
    public ServerResponse bindParent(HttpServletRequest request, String account) {
        return this.iUserService.bindParent(request, account);
    }


    /**
     * 获取上级
     * @param request
     * @return
     */
    @RequestMapping({"getParentUser.do"})
    @ResponseBody
    public ServerResponse getParentUser(HttpServletRequest request) {
        return this.iUserService.getParentUser(request);
    }


    /**
     * 获取下级
     * @param request
     * @return
     */
    @RequestMapping({"listChildUser.do"})
    @ResponseBody
    public ServerResponse listChildUser(HttpServletRequest request) {
        return this.iUserService.listChildUser(request);
    }


    /**
     * 根据 parentId 获取直属下级
     * @param userId
     * @return
     */
    @RequestMapping({"listChildUserByParentId.do"})
    @ResponseBody
    public ServerResponse listChildUserByParentId(Integer userId, HttpServletRequest request) {
        return this.iUserService.listChildUserByParentId(userId, request);
    }


    /**
     * 获取配资设置
     * @return
     */
    @RequestMapping({"getAllocationSetting.do"})
    @ResponseBody
    public ServerResponse getAllocationSetting() {
        return ServerResponse.createBySuccess(this.stAllocationSettingService.listSetting());
    }

}

